import api from './index'

export interface DetectionReport {
  id: number
  document_id: number
  file_url: string
  cover_image_url: string
  is_parsed: boolean
  is_summarized: boolean
  file: string
  file_name: string
  page_count: number
  cover_image: string
  parsing_status: string
  parsing_progress: number
  parsing_error: string
  summary_status: string
  visible: boolean
  created_at: string
  updated_at: string
  summary?: string
  html?: string
  text?: string
  total_text?: string
  parsing: boolean
  summarizing: boolean
}

export interface ParseDocumentResponse {
  success: boolean
  message: string
  document_id: number
}

// 上传文档
export interface UploadDetectionReportResponse {
  success: boolean
  message: string
  document_id: number
}

// 总结文档
export interface SummarizeDocumentResponse {
  success: boolean
  message: string
}

// 获取文档列表
export const getDetectionReports = (): Promise<DetectionReport[]> => {
  return api.get('/detection-reports/')
}

// 获取单个文档
export const getDetectionReport = (id: number): Promise<DetectionReport> => {
  return api.get(`/detection-reports/${id}/`)
}

// 解析文档
export const parseDetectionReport = (id: number): Promise<ParseDocumentResponse> => {
  return api.post(`/parse-detection-report/`, {
    detection_report_id: id,
  })
}

// 解析原始文档
export const parseOriginDocument = (id: number): Promise<ParseDocumentResponse> => {
  return api.post(`/parse-document/`, {
    document_id: id,
  })
}

// 总结文档
export const summarizeDetectionReport = (id: number): Promise<SummarizeDocumentResponse> => {
  return api.post(`/summarize-detection-report/`, {
    detection_report_id: id,
  })
}

export const uploadDetectReport = (file: File): Promise<UploadDetectionReportResponse> => {
  const formData = new FormData()
  formData.append('file', file)

  return api.post('/upload-detection-report/', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}
